import 'dart:math';

import 'package:flutter/cupertino.dart';

class JRGradientPainter extends CustomPainter {
  final Gradient gradient;
  JRGradientPainter(this.gradient);
  @override
  void paint(Canvas canvas, Size size) {
    double minSize = min(size.width, size.height);
    double scaleW = 1.0;
    double scaleH = 1.0;
    if (minSize != 0) {
      scaleW = size.width / minSize;
      scaleH = size.height / minSize;
    }

    final Rect rect =
        Rect.fromPoints(const Offset(0, 0), Offset(minSize, minSize));
    Shader shader =
        gradient.createShader(rect, textDirection: TextDirection.ltr);
    final Paint paint = Paint()..shader = shader;
    final Matrix4 matrix = Matrix4.identity()..scale(scaleW, scaleH);
    canvas.save();
    canvas.transform(matrix.storage);
    canvas.drawRect(rect, paint);
    canvas.restore();
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
